﻿#region Dont Look This

// /*************************************************************************************
//
// * 文件名称：  Service1.cs
//
// * 作       者：  jieaido@outlook.com
//
// * 创建日期：  2020/04/02/20:54
//
// * 修改日期:     2020/04/06/20:54
//
// * 功能描述：
//
//
//
//                               Copyright (c) Jieaido. AllRights Reserved
//
// *************************************************************************************/

#endregion Dont Look This

using Common.Logging;
using HelpLibrary;
using HelpLibrary.MyConfig;
using HelpLibrary.MyLog;
using HelpLibrary.OPC;
using HelpLibrary.OPCHelper;
using OPCAutomation;
using Quartz;
using Quartz.Impl;
using System;
using System.ServiceProcess;
using System.Threading;

namespace RSServer
{
    public partial class Service1 : ServiceBase
    {
        private string filePath;

        public Service1()
        {
            InitializeComponent();
            //filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "RsServerLog.log");
        }

        public OPCItem opcItem { get; set; }

        public Thread threadforwork { get; set; }

        protected override void OnStart(string[] args)
        {
            if (threadforwork == null) threadforwork = new Thread(workFunction);
            threadforwork.IsBackground = true;
            threadforwork.Start();

            //using (FileStream stream = new FileStream(filePath, FileMode.Append))
            //using (StreamWriter writer = new StreamWriter(stream))
            //{
            //    writer.WriteLine($"{DateTime.Now},服务启动！");
            //}
            MyLogHelper.LogHelper.WriteInfo($"{DateTime.Now},服务启动！");
        }

        private void workFunction()
        {
            Thread.Sleep(5000);
            var mc = new MyConfig();
            var itemtag = mc.GetTag();
            var linkState = MyOPCHelper.Instance.Connect(mc.GetServerName());

            if (linkState == OPCServerState.OPCRunning)
            {
                MyLogHelper.LogHelper.WriteInfo(" OPC Server Connect Success!");
                MyOPCHelper.Instance.CreateOPCGroup(false);
                opcItem = MyOPCHelper.Instance.AddItem(itemtag);
            }

            ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
            var scheduler = schedulerFactory.GetScheduler();
            scheduler.Start();
        }

        protected override void OnStop()
        {
            //using (FileStream stream = new FileStream(filePath, FileMode.Append))
            //using (StreamWriter writer = new StreamWriter(stream))
            //{
            //    writer.WriteLine($"{DateTime.Now},服务停止！");
            //}
            MyOPCHelper.Instance.ReleaseOPC();
            MyLogHelper.LogHelper.WriteInfo($"{DateTime.Now},服务停止！");
        }
    }

    public class HelloJob : IJob
    {
        public static ILog logObj = LogManager.GetLogger<HelloJob>();

        public void Execute(IJobExecutionContext context)
        {
            object ItemValues;
            object Qualities;
            object TimeStamps;
            MyOPCItems.CurrentopcItem.Read(1, out ItemValues, out Qualities, out TimeStamps);
            DbHelp.Add(ItemValues.ToString());
            // var s = MyOPCItems.Bindlist.OrderBy(a => a.ItemValue);
            //var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "RsServerLog2.log");
            //using (FileStream stream = new FileStream(filePath, FileMode.Append))
            //using (StreamWriter writer = new StreamWriter(stream))
            //{
            //    foreach (var myOPCItem in s)
            //    {
            //        // writer.WriteLine($"{DateTime.Now},服务停止！");
            //        MyLogHelper.LogHelper.WriteError($"{myOPCItem.ItemTag}-is-{myOPCItem.ItemValue}-at-{DateTime.Now}");
            //    }

            //}
        }
    }
}